// 实习材料Repository
package org.example.backend.repository;

import org.example.backend.entity.InternshipMaterial;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface InternshipMaterialRepository extends JpaRepository<InternshipMaterial, Long> {
    Page<InternshipMaterial> findByStudentId(Long studentId, Pageable pageable);

    List<InternshipMaterial> findByInternshipId(Long internshipId);

    Page<InternshipMaterial> findByInternshipId(Long internshipId, Pageable pageable);

    @Query("SELECT im FROM InternshipMaterial im WHERE im.studentId = :studentId AND (:type IS NULL OR im.type = :type)")
    Page<InternshipMaterial> findByStudentIdAndType(@Param("studentId") Long studentId,
                                                   @Param("type") Integer type,
                                                   Pageable pageable);

    @Query("SELECT im FROM InternshipMaterial im WHERE im.internshipId IN :internshipIds")
    List<InternshipMaterial> findByInternshipIds(@Param("internshipIds") List<Long> internshipIds);

    @Query("SELECT im FROM InternshipMaterial im WHERE (:status IS NULL OR im.status = :status)")
    Page<InternshipMaterial> findByStatus(@Param("status") Integer status, Pageable pageable);

    @Query("SELECT im FROM InternshipMaterial im JOIN Internship i ON im.internshipId = i.id " +
            "WHERE i.teacherId = :teacherId AND im.status = :status")
    Page<InternshipMaterial> findByTeacherIdAndStatus(@Param("teacherId") Long teacherId,
                                                      @Param("status") Integer status,
                                                      Pageable pageable);
}
